package com.cqc.tabactivity01;

import android.app.ActivityGroup;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;

/**
 * TabActivity切换tab
 * 1 获取TabHost对象
 * 2 创建tab
 */
public class MainActivity extends ActivityGroup {

    private static final String TAG = "MainActivity";
    private Context context;
    private TabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        tabHost = (TabHost) findViewById(R.id.mytabhost);
        tabHost.setup(getLocalActivityManager());//正确
//        tabHost.setup();//错误

        LayoutInflater inflater = LayoutInflater.from(context);
        View tab1View = inflater.inflate(R.layout.tab1, null);
        View tab2View = inflater.inflate(R.layout.tab2, null);
        View tab3View = inflater.inflate(R.layout.tab3, null);
        TabHost.TabSpec tab1 = tabHost.newTabSpec("Tab1Tag").setIndicator(tab1View).setContent(new Intent(context, Activity_A.class));
        TabHost.TabSpec tab2 = tabHost.newTabSpec("Tab2Tag").setIndicator(tab2View).setContent(new Intent(context, Activity_B.class));
        TabHost.TabSpec tab3 = tabHost.newTabSpec("Tab3Tag").setIndicator(tab3View).setContent(new Intent(context, Activity_C.class));

        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);

        tabHost.setCurrentTab(0);

        //不加监听，也可以切换各activity
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                switch (s) {
                    case "Tab1Tag":
                        Log.d(TAG, "Tab1");
                        break;
                    case "Tab2Tag":
                        Log.d(TAG, "Tab2");
                        break;
                    case "Tab3Tag":
                        Log.d(TAG, "Tab3");
                        break;
                }
            }
        });
    }
}
